package com.bdc.whatwhat.utils;

import android.content.Context;
import android.util.Log;

import com.bdc.whatwhat.R;

import org.joda.time.DateTime;
import org.joda.time.Days;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by gfimac on 08/08/2014.
 */
public class DateUtils {

    static String FORMAT_DATE_ISO = "yyyy-MM-dd HH:mm:ss";

    public static Date toDate(String s){
        DateFormat f = new SimpleDateFormat(FORMAT_DATE_ISO);
        try {
            return f.parse(s);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String formaterDate(String s, Context c){
        Date d = toDate(s);
        if(d == null)
            return "";

        Date now = new Date();
        now.setHours(12);
        now.setMinutes(0);
        now.setSeconds(0);

        Date d2 = new Date();
        d2.setTime(d.getTime());
        d2.setHours(12);
        d2.setMinutes(0);
        d2.setSeconds(0);


        long diffDays = (now.getTime() - d2.getTime()) / (24 * 60 * 60 * 1000);

        Log.d("date", d.toString());

        int diffHours = now.getHours()-d.getHours();
        int diffMinutes = now.getMinutes()-d.getMinutes();

        Log.d("diffDays", diffDays+"");

        switch((int)diffDays){
            case 0:
                if(d.getMinutes()<10)
                    return d.getHours()+":0"+d.getMinutes();
                else
                    return d.getHours()+":"+d.getMinutes();
                /*
                if(diffHours>=1)
                    return c.getResources().getString(R.string.il_y_a_x_heures).replace("#x#",""+diffHours);
                else
                    return c.getResources().getString(R.string.il_y_a_x_minutes).replace("#x#",""+diffMinutes);
                    */
            case 1:
                return c.getResources().getString(R.string.hier);
            default:
                return c.getResources().getString(R.string.il_y_a_x_jours).replace("#x#",""+diffDays);

        }
    }

}
